layui.use('form', function(){
	let form = layui.form;
	let data = {
		username: '',
		password: '',
		verify: ''
	};
	//生产验证码事件
	function imgVerify(){
	$('#imgVerify').attr("src",ctx+'captcha');
	}
	imgVerify();

	//验证码点击刷新事件
	$('#imgVerify').click(function(){
	$(this).attr("src",ctx+'captcha?t=' + (new Date).getTime());
	});
	
	
	//提交表单 这两句话要加上，跨域请求保持一个session
	// xhrFields: {
	// 		   withCredentials: true
	// 	   },
	//  crossDomain: true, 
	$('#submit').click(function(){
		data.username = $('#username').val();
		data.password = $('#password').val();
		data.verify = $('#verify').val();	
			$.ajax({
				type: "POST",
				url: ctx+"login",
				xhrFields: {
				           withCredentials: true
				       },
				crossDomain: true,	   
				data: data,
				success: function (result, status, xhr) {
				   if(result.code == 200){
					   var token = xhr.getResponseHeader("token");
					   //此方法用于同一用户不同页面传递值
					   window.sessionStorage.setItem("access_token", token);
					   //这种写法跳转页面后浏览器不会有返回按钮
					   window.location.replace("index.html");
				   }else{
					   layer.msg(result.msg,{icon: 5, shift: 6});
				   }
				},
				error: function(result){
					layer.msg("登录失败！")
				}
			});
	})

	
});